#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};



TreeNode* dfs(vector<int>& nums, int left, int right)
{
    if (left > right) return nullptr;

    int maxValueIndex = left;
    for (int i = left + 1; i <= right; i++)
    {
        if (nums[maxValueIndex] < nums[i])
        {
            maxValueIndex = i;
        }
    }

    TreeNode* root = new TreeNode(nums[maxValueIndex]);

    root->left = dfs(nums, left, maxValueIndex - 1);
    root->right = dfs(nums, maxValueIndex + 1, right);

    return root;
}